package com.sinosoft.perfect.system._theme.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TTheme;
import com.sinosoft.perfect.system._.po.TUser;
import com.sinosoft.perfect.system._theme.dataFormat.IThemeGridDataFormat;
import com.sinosoft.perfect.system._theme.service.IThemeService;
import com.sinosoft.perfect.system._user.service.IUserService;

@SuppressWarnings("serial")
public class ThemeAction extends BaseAction {

	private IThemeService themeService;

	private IUserService userService;

	private IThemeGridDataFormat themeGridDataFormat;

	private String userId;

	public IThemeService getThemeService() {
		return themeService;
	}

	public void setThemeService(IThemeService themeService) {
		this.themeService = themeService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IThemeGridDataFormat getThemeGridDataFormat() {
		return themeGridDataFormat;
	}

	public void setThemeGridDataFormat(IThemeGridDataFormat themeGridDataFormat) {
		this.themeGridDataFormat = themeGridDataFormat;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String themeGrid() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TTheme> themes = themeService.selectByTTheme(null);
			resultXml = themeGridDataFormat.dataFormatThemeGrid(themes, getSelectThemeId());
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	private String getSelectThemeId() throws SQLException{
		String selectThemeId = null;
		if(userId != null){
			TUser user = new TUser();
			user.setUserMainId(userId);
			List<TUser> users = userService.selectTUser(user);
			if (users != null && users.size() != 0) {
				selectThemeId = users.get(0).getThemeId();
			}
		}
		return selectThemeId;
	}

}
